// ToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "Redirect.h"
#include "WinPlace.h"
#include "afxadv.h"
#include "ToolBar.h"
#include "Resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Constantes

#define COMBOBOX_INDEX   15
#define COMBOBOX_WIDTH  250
#define COMBOBOX_HEIGHT 150
#define COMBOBOX_BITMAP   4

static UINT BASED_CODE ToolButtons[] =
{
	// same order as in the bitmap 'ToolBar1.bmp'
	ID_FILE_SAVE_AS,
		ID_SEPARATOR,
	ID_EDIT_CUT,
	ID_EDIT_COPY,
	ID_EDIT_PASTE,
		ID_SEPARATOR,
	ID_EDIT_CLEAR_ALL,
	ID_EDIT_FIND,
		ID_SEPARATOR,
	ID_FILE_PRINT_PREVIEW,
	ID_FILE_PRINT,
		ID_SEPARATOR,
	
		ID_SEPARATOR,

		ID_SEPARATOR,
	ID_FILE_OPEN,
};

/////////////////////////////////////////////////////////////////////////////
// CMyToolBar

CMyToolBar::CMyToolBar()
{
	m_nColumns = 2;
}

CMyToolBar::~CMyToolBar()
{
    if (m_Font.m_hObject != NULL)
		m_Font.DeleteObject();
}

BEGIN_MESSAGE_MAP(CMyToolBar, CToolBar)
	//{{AFX_MSG_MAP(CMyToolBar)
	ON_CBN_SELCHANGE(ID_PROCESS_LIST, OnProcessList)
	ON_UPDATE_COMMAND_UI(ID_PROCESS_LIST, OnUpdateProcessList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CMyToolBar::Init(CWnd* pParentWnd, CRecentFileList *pRecentFileList, LPCTSTR lpszCmdLine)
{
	// start out with no borders
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_DYNAMIC;
	dwStyle |= (CBRS_TOOLTIPS | CBRS_FLYBY);			// add tooltips
	if (!Create(pParentWnd, dwStyle, AFX_IDW_TOOLBAR))
		return FALSE;
	
	if (!LoadBitmap(IDB_BITMAP1))
		return FALSE;
	
	if (!SetButtons(ToolButtons, sizeof(ToolButtons) / sizeof(UINT)))
		return FALSE;
	
	CRect rect(-COMBOBOX_WIDTH, -COMBOBOX_HEIGHT, 0, 0);
	
	// The ID of the ComboBox is important for two reasons.  One, so you
	// can receive notifications from the control.  And also for ToolTips.
	// During HitTesting if the ToolBar sees that the mouse is one a child
	// control, the toolbar will lookup the controls ID and search for a
	// string in the string table with the same ID to use for ToolTips
	// and StatusBar info.
	if (!m_ProcessList.Create(CBS_DROPDOWNLIST|WS_VSCROLL|WS_CHILD, rect, this, ID_PROCESS_LIST))
		return FALSE;

	LOGFONT logfont;
	memset(&logfont, 0, sizeof(logfont));
	_tcscpy(logfont.lfFaceName, _T("Arial"));
	CWindowDC dc(NULL);
	logfont.lfHeight = -MulDiv(8, dc.GetDeviceCaps(LOGPIXELSY), 72);
	logfont.lfWeight = FW_NORMAL;
	m_Font.CreateFontIndirect(&logfont);
	m_ProcessList.SetFont(&m_Font);
	
	if (!SetHorizontal())
		return FALSE;
	
	UpdateFileList(pRecentFileList, lpszCmdLine);
	
	return TRUE;
}

BOOL CMyToolBar::SetHorizontal()
{
	m_bVertical = FALSE;
	
	SetBarStyle(GetBarStyle() | CBRS_ALIGN_TOP);
	
	SetButtonInfo(COMBOBOX_INDEX, ID_PROCESS_LIST, TBBS_SEPARATOR, COMBOBOX_WIDTH);
	
	if (m_ProcessList.m_hWnd != NULL)
	{
		CRect rect;
		GetItemRect(COMBOBOX_INDEX, rect);
		
		m_ProcessList.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_ProcessList.ShowWindow(SW_SHOW);
	}
	return TRUE;
}

BOOL CMyToolBar::SetVertical()
{
	m_bVertical = TRUE;
	SetButtonInfo(COMBOBOX_INDEX, ID_EDIT_FIND, TBBS_BUTTON, COMBOBOX_BITMAP);
	
	if (m_ProcessList.m_hWnd != NULL)
		m_ProcessList.ShowWindow(SW_HIDE);
	
	return TRUE;
}

CSize CMyToolBar::CalcDynamicLayout(int nLength, DWORD dwMode)
{
	// if we're committing set the buttons appropriately
	if (dwMode & LM_COMMIT)
	{
		if (dwMode & LM_VERTDOCK)
		{
			if (!m_bVertical)
				SetVertical();
		}
		else
		{
			if (m_bVertical)
				SetHorizontal();
		}
		
		return CToolBar::CalcDynamicLayout(nLength, dwMode);
	}
	else
	{
		BOOL bOld = m_bVertical;
		BOOL bSwitch = (dwMode & LM_HORZ) ? bOld : !bOld;
		
		if (bSwitch)
		{
			if (bOld)
				SetHorizontal();
			else
				SetVertical();
		}
		
		CSize sizeResult = CToolBar::CalcDynamicLayout(nLength, dwMode);
		
		if (bSwitch)
		{
			if (bOld)
				SetHorizontal();
			else
				SetVertical();
		}
		
		return sizeResult;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMyToolBar message handlers

void CMyToolBar::SetColumns(UINT nColumns)
{
	m_nColumns = nColumns;
	int nCount = GetButtonCount();
	
	for (int i = 0; i < nCount; i++)
	{
		UINT nStyle = GetButtonStyle(i);
		BOOL bWrap = (((i + 1) % nColumns) == 0);
		if (bWrap)
			nStyle |= TBBS_WRAPPED;
		else
			nStyle &= ~TBBS_WRAPPED;
		SetButtonStyle(i, nStyle);
	}
	
	Invalidate();
	GetParentFrame()->RecalcLayout();
}

int CMyToolBar::GetButtonCount()
{
	return GetToolBarCtrl().GetButtonCount();
}

void CMyToolBar::UpdateFileList(CRecentFileList *pRecentFileList, LPCTSTR lpszCmdLine)
{
	// fill the m_ProcessList
	m_ProcessList.ResetContent();
	int Size = pRecentFileList->GetSize();
	for (int i = 0; i < Size; i++)
	{
		CString strFileName = (*pRecentFileList)[i];
		if (!strFileName.IsEmpty())
			m_ProcessList.AddString(strFileName);
	}
	m_ProcessList.SelectString(0, lpszCmdLine);
}

void CMyToolBar::OnProcessList() 
{
	int CurSel = m_ProcessList.GetCurSel();
	if (CurSel != CB_ERR)
	{
		CString strCmdLine;
		m_ProcessList.GetLBText(CurSel, strCmdLine);
	
	}
}

void CMyToolBar::OnUpdateProcessList(CCmdUI* pCmdUI) 
{
	//pCmdUI->Enable(!theApp.IsChildRunning());
}

